﻿$.kish = {}
$(function () {
    $.kish.message = {};
    $.extend($.kish.message, {
        getErrorMessage: function (code) {
            $.kish.message.get(code, 0);
        },

        showErrorMessage: function (message) {
            $.kish.message.alertError(message, 0);
        },
        getErrorMessageList: function (codes) {
            $.kish.message.List(codes, 0);
        },
        getUserMessage: function (code) {
            $.kish.message.get(code, 1);
        },

        showUserMessage: function (message) {
            $.kish.message.alertMessage(message, 1);
        },
        getConfirmationMessage: function (code, fnOk, fnCancel) {
            $.kish.message.get(code, 2, fnOk, fnCancel);
        },
        get: function (code, type, fnOk, fnCancel) {

            $.ajax({
                'type': 'POST',
                'url': '../../Services/Message.svc/Get',
                'data': JSON.stringify({ message: { "Code": code, "Type": type} }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (data) {
                    var messageText = "";
                    $.each(data, function () {
                        messageText = messageText + this.Message;
                    });
                    messageText = messageText
                    switch (type) {
                        case 0:
                            $.kish.message.alertError(messageText, type);
                            break;
                        case 1:
                            $.kish.message.alertMessage(messageText, type);
                            break;
                        case 2:
                            $.kish.message.alertConfirmation(messageText, fnOk, fnCancel);
                            break;
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });

        },
        List: function (codes, type, fnOk, fnCancel) {
            var messages = [];
            for (i = 0; i < codes.length; i++)
                messages.push({ "Code": codes[i], "Type": type });
            $.ajax({
                'type': 'POST',
                'url': "../../Services/Internal/Message.svc/List",
                'data': JSON.stringify({ messages: messages }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (data) {
                    var messageText = "<ul>";
                    $.each(data, function () {
                        messageText = messageText + "<li>" + this.Message + "</li>";
                    });
                    messageText = messageText + "</ul>"
                    switch (type) {
                        case 0:
                            $.kish.message.alertError(messageText, type);
                            break;
                        case 1:
                            $.kish.message.alertMessage(messageText, type);
                            break;
                        case 2:
                            $.kish.message.alertConfirmation(messageText, fnOk, fnCancel);
                            break;
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });
        },

        alertMessage: function (data, type) {
            $.kish.message.showMessage(".commonMessage", data, type)
        },
        alertValidationMessage: function (elem) {
            var messageDiv = $(".commonMessage", ".MessageTemplates").clone();
            $(".content", messageDiv).empty();
            html = "<ul>";
            $.each($('.invalid', elem), function () {
                $.each($(this).data('invalids'), function () {
                    if (html.search(this) == -1)
                        html = html + "<li>" + this + "</li>";
                });
            });
            html = html + "</ul>";
            $(messageDiv).attr('title', "توجه");
            $(".content", messageDiv).html('<div class="validationMessage">' + html + '</div>');
            $(".content", messageDiv).css('color', 'red');
            $(messageDiv).dialog({
                minWidth: 300,
                autoOpen: false,
                show: "blind",
                buttons: {
                    Ok: function () {
                        $(this).dialog("close");
                    }
                }
            });
            $.kish.message.openDialog(messageDiv);
        },
        fixButtons: function (messageDiv) {
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Yes")').html('<img alt="Yes"  title="Yes" src="../Images/ico/icon_f_approval.png">');
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Ok")').html('<img alt="OK" title="OK" src="../Images/ico/icon_f_approval.png">');
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Cancel")').html('<img alt="Cancel" title="Cancel" src="../Images/ico/icon_f_close.png">');
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("No")').html('<img title="No" alt="No" src="../Images/ico/icon_f_close.png">');
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Next")').html('<img title="Next" src="../Images/ico/icon_e_next.png">');
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Add")').html('<img title="Add" src="../Images/ico/icon_f_add.png">');
            //            $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Delete")').html('<img title="Delete" src="../Images/ico/icon_f_delete.gif">');
            //            if ($('.ui-dialog-buttonpane', messageDiv).find('button:contains("Submit")').length > 0) {
            //                $('.ui-dialog-buttonpane .ui-button', messageDiv).css('width', '80px');
            //                $('.ui-dialog-buttonpane', messageDiv).find('button:contains("Submit")').html('<div class="saveTable_fn right margin_t_4 button width60">Submit</div>');
            //            }
            //            else
            //                $('.ui-dialog-buttonpane .ui-button', messageDiv).css('width', '20px');
        },
        openDialog: function (messageDiv) {
            $.kish.message.fixButtons($(messageDiv).parent());
            $(messageDiv).dialog("open");
        },
        alertConfirmation: function (message, fnOk, fnCancel) {
            var messageDiv = $(".commonMessage", ".MessageTemplates").clone();
            $(messageDiv).attr('title', 'تایید');
            $(".content", messageDiv).empty();
            $(".content", messageDiv).text(message);
            $(".content", messageDiv).css('color', '#FF6600');
            $(messageDiv).dialog({
                modal: true,
                autoOpen: false,
                show: "blind",
                minWidth: 100,
                buttons: {
                    خیر: function () {
                        fnCancel();
                        $(this).dialog("close");
                    },
                    بله: function () {
                        fnOk();
                        $(this).dialog("close");
                    }

                }
            });
            $.kish.message.openDialog(messageDiv);
        },
        alertError: function (data, type) {
            $.kish.message.showMessage(".commonMessage", data, type);
        },
        showMessage: function (elem, data, type) {
            var messageDiv = $(elem, ".MessageTemplates").clone();
            $(".content", messageDiv).empty();
            $(".content", messageDiv).html(data);
            switch (type) {
                case 0:
                    $(".content", messageDiv).css('color', 'red');
                    $(messageDiv).attr('title', 'خطا');
                    break;
                case 1:
                    $(".content", messageDiv).css('color', 'green');
                    $(messageDiv).attr('title', 'Success');
                    break;
            }
            $(messageDiv).dialog({
                modal: true,
                autoOpen: false,
                minWidth: 100,
                show: "blind",
                buttons: {
                    Ok: function () {
                        $(this).dialog("close");
                    }
                }
            });
            $.kish.message.openDialog(messageDiv);
        }

    });
});